#include "simplekeyboard.h"
#include <QTextEdit>
#include <QDebug>

SimpleKeyboard::SimpleKeyboard(QWidget *parent)
    : QWidget(parent)
    , m_inputLine(nullptr)
    , m_targetWidget(nullptr)
{
    setupUI();
    // 作为工具窗口，始终置顶并跟随父窗口堆叠
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    
    // 设置固定尺寸
    setFixedSize(800, 250);
}

SimpleKeyboard::~SimpleKeyboard()
{
}

void SimpleKeyboard::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(5, 5, 5, 5);
    mainLayout->setSpacing(5);
    
    setStyleSheet("QWidget { background-color: #34495e; }"
                  "QPushButton { background-color: #ecf0f1; color: #2c3e50; border: 1px solid #bdc3c7; "
                  "border-radius: 5px; font-size: 16px; font-weight: bold; min-height: 45px; }"
                  "QPushButton:pressed { background-color: #3498db; color: white; }"
                  "QLineEdit { background-color: white; color: black; border: 2px solid #3498db; "
                  "padding: 5px; font-size: 14px; }");
    
    // 输入显示行
    m_inputLine = new QLineEdit(this);
    m_inputLine->setReadOnly(true);
    m_inputLine->setFixedHeight(40);
    mainLayout->addWidget(m_inputLine);
    
    // 第一行：数字键
    QGridLayout *numLayout = new QGridLayout();
    numLayout->setSpacing(5);
    QStringList numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    for (int i = 0; i < numbers.size(); ++i) {
        QPushButton *btn = new QPushButton(numbers[i], this);
        connect(btn, &QPushButton::clicked, this, &SimpleKeyboard::onKeyClicked);
        numLayout->addWidget(btn, 0, i);
        m_keyButtons.append(btn);
    }
    mainLayout->addLayout(numLayout);
    
    // 第二行：十六进制字母
    QGridLayout *hexLayout = new QGridLayout();
    hexLayout->setSpacing(5);
    QStringList hexChars = {"A", "B", "C", "D", "E", "F"};
    for (int i = 0; i < hexChars.size(); ++i) {
        QPushButton *btn = new QPushButton(hexChars[i], this);
        connect(btn, &QPushButton::clicked, this, &SimpleKeyboard::onKeyClicked);
        hexLayout->addWidget(btn, 0, i);
        m_keyButtons.append(btn);
    }
    mainLayout->addLayout(hexLayout);
    
    // 第三行：常用字符
    QGridLayout *charLayout = new QGridLayout();
    charLayout->setSpacing(5);
    QStringList chars = {"AT", "+", "-", "=", "?", "!", ":", ";"};
    for (int i = 0; i < chars.size(); ++i) {
        QPushButton *btn = new QPushButton(chars[i], this);
        connect(btn, &QPushButton::clicked, this, &SimpleKeyboard::onKeyClicked);
        charLayout->addWidget(btn, 0, i);
        m_keyButtons.append(btn);
    }
    mainLayout->addLayout(charLayout);
    
    // 第四行：空格和控制键
    QHBoxLayout *controlLayout = new QHBoxLayout();
    controlLayout->setSpacing(5);
    
    QPushButton *backspaceBtn = new QPushButton("删除", this);
    backspaceBtn->setStyleSheet("QPushButton { background-color: #e74c3c; color: white; }");
    connect(backspaceBtn, &QPushButton::clicked, this, &SimpleKeyboard::onBackspace);
    controlLayout->addWidget(backspaceBtn);
    
    QPushButton *spaceBtn = new QPushButton("空格", this);
    connect(spaceBtn, &QPushButton::clicked, [this]() {
        insertText(" ");
    });
    controlLayout->addWidget(spaceBtn, 2);
    
    QPushButton *clearBtn = new QPushButton("清空", this);
    clearBtn->setStyleSheet("QPushButton { background-color: #f39c12; color: white; }");
    connect(clearBtn, &QPushButton::clicked, this, &SimpleKeyboard::onClear);
    controlLayout->addWidget(clearBtn);
    
    QPushButton *enterBtn = new QPushButton("完成", this);
    enterBtn->setStyleSheet("QPushButton { background-color: #27ae60; color: white; }");
    connect(enterBtn, &QPushButton::clicked, this, &SimpleKeyboard::onEnter);
    controlLayout->addWidget(enterBtn);
    
    mainLayout->addLayout(controlLayout);
}

void SimpleKeyboard::setTargetWidget(QWidget *target)
{
    m_targetWidget = target;
}

QString SimpleKeyboard::getText() const
{
    return m_inputLine->text();
}

void SimpleKeyboard::setText(const QString &text)
{
    m_inputLine->setText(text);
}

void SimpleKeyboard::onKeyClicked()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (btn) {
        qDebug() << "[SimpleKeyboard] 按键点击：" << btn->text();
        insertText(btn->text());
    }
}

void SimpleKeyboard::insertText(const QString &text)
{
    m_inputLine->setText(m_inputLine->text() + text);
    emit textChanged(m_inputLine->text());
}

void SimpleKeyboard::onBackspace()
{
    QString current = m_inputLine->text();
    if (!current.isEmpty()) {
        current.chop(1);
        m_inputLine->setText(current);
        emit textChanged(current);
    }
}

void SimpleKeyboard::onClear()
{
    m_inputLine->clear();
    emit textChanged("");
}

void SimpleKeyboard::onEnter()
{
    qDebug() << "[SimpleKeyboard] 完成按钮被点击，当前文本：" << m_inputLine->text();
    
    // 发送信号，由外部处理文本传递和界面状态
    emit enterPressed();
    
    // 清空输入行（可选，根据需求决定是否保留）
    m_inputLine->clear();
}

